package com.kuwei.servlet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.kuwei.base.ParamerException;
import com.kuwei.base.ServiceException;
import com.kuwei.service.TestService;
import com.kuwei.servlet.frame.BaseServlet;
import com.kuwei.utils.CommonUtils;
import com.kuwei.utils.Constants;
import com.kuwei.utils.ReturnResult;
/**
 * 测试Servlet
 * @author zyc
 *
 */
@Service("testServ")
public class TestServlet extends BaseServlet{
	public TestServlet() {
		
	}
	@Autowired
	public TestService testService;
	
	public void queryTest(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if(CommonUtils.isEmpty(id)){
			paramMap.put("id", id);
			try {
				List<Map<String, Object>> list = testService.queryTest(paramMap);
				result.setContent(Constants.SUCCESS,JSONObject.toJSONString(list).toString());
			} catch (ServiceException e) {
				result.setContent(Constants.FAIL, e.getMessage());
				throw new ServiceException(this.getClass().toString()+" \n ->"+e.getMessage(),e);
			} catch (Exception e){
				result.setContent(Constants.FAIL, e.getMessage());
			}
		} else{
			throw new ParamerException("Parameter[id] is empty, please check it!");
		}
		
		rtnMap.put("data", result.getContent());
	}
	
	public void insertTest(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		String name = request.getParameter("name");
		if(CommonUtils.isEmpty(name)){
			paramMap.put("name", name);
			try {
				int rtnNum= testService.insertTest(paramMap);
				result.setContent(Constants.SUCCESS,String.valueOf(rtnNum));
			} catch (ServiceException e) {
				result.setContent(Constants.FAIL, e.getMessage());
				throw new ServiceException(this.getClass().toString()+" \n ->"+e.getMessage(),e);
			} catch (Exception e){
				result.setContent(Constants.FAIL, e.getMessage());
			}
		} else{
			throw new ParamerException("Parameter[name] is empty, please check it!");
		}
		
		rtnMap.put("data", result.getContent());
	}
	
	public void updateTest(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		if(CommonUtils.isEmpty(id)&&CommonUtils.isEmpty(name)){
			paramMap.put("name", name);
			paramMap.put("id", id);
			try {
				int rtnNum= testService.updateTest(paramMap);
				result.setContent(Constants.SUCCESS,String.valueOf(rtnNum));
			} catch (ServiceException e) {
				result.setContent(Constants.FAIL, e.getMessage());
				throw new ServiceException(this.getClass().toString()+" \n -> "+e.getMessage(),e);
			} catch (Exception e){
				result.setContent(Constants.FAIL, e.getMessage());
			}
		} else{
			throw new ParamerException("Parameter[id || name] is empty, please check it!");
		}
		
		rtnMap.put("data", result.getContent());
	}
	
	public void deleteTest(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if(CommonUtils.isEmpty(id)){
			paramMap.put("id", request.getParameter("id"));
			try {
				int rtnNum= testService.deleteTest(paramMap);
				result.setContent(Constants.SUCCESS,String.valueOf(rtnNum));
			} catch (ServiceException e) {
				result.setContent(Constants.FAIL, e.getMessage());
				throw new ServiceException(this.getClass().toString()+"-> \n "+e.getMessage(),e);
			} catch (Exception e){
				result.setContent(Constants.FAIL, e.getMessage());
			}
		} else{
			throw new ParamerException("Parameter[id] is empty, please check it!");
		}
		
		rtnMap.put("data", result.getContent());
	}
}
